local G  = _G 

local jass = require 'jass.common'
local storm = require 'jass.storm'
local japi = require 'jass.japi'
local slk = require 'jass.slk'


local mt = {}
setmetatable(G, mt)
mt.__index = mt

-- 注册jass
mt['jass'] = setmetatable({}, {__index = function(self, name)
    local func = jass[name]
    if func then 
        rawset(self, name, jass[name])
        return func
    end 
end,
})

-- 注册japi
mt['japi'] = setmetatable({}, {__index = function(self, name)
    local func = japi[name]
    if func then 
        rawset(self, name, func)
        -- print(name, "注册japi函数")
        return func
    end 
end,
__newindex = function(self, name, value)
    if type(value ) == 'function' then 
        rawset(japi, name, value)
    end 
end,
})


register_japi [[
    
    // AI 函数
    native UnitAlive takes unit whichUnit returns boolean

    native DzGetMouseTerrainX takes nothing returns real
    native DzGetMouseTerrainY takes nothing returns real
    native DzGetMouseTerrainZ takes nothing returns real
    native DzIsMouseOverUI takes nothing returns boolean
    native DzGetMouseX takes nothing returns integer
    native DzGetMouseY takes nothing returns integer
    native DzGetMouseXRelative takes nothing returns integer
    native DzGetMouseYRelative takes nothing returns integer
    native DzSetMousePos takes integer x, integer y returns nothing
    native DzTriggerRegisterMouseEvent takes trigger trig, integer btn, integer status, boolean sync, string func returns nothing
    native DzTriggerRegisterMouseEventByCode takes trigger trig, integer btn, integer status, boolean sync, code funcHandle returns nothing
    native DzTriggerRegisterKeyEvent takes trigger trig, integer key, integer status, boolean sync, string func returns nothing
    native DzTriggerRegisterKeyEventByCode takes trigger trig, integer key, integer status, boolean sync, code funcHandle returns nothing
    native DzTriggerRegisterMouseWheelEvent takes trigger trig, boolean sync, string func returns nothing
    native DzTriggerRegisterMouseWheelEventByCode takes trigger trig, boolean sync, code funcHandle returns nothing
    native DzTriggerRegisterMouseMoveEvent takes trigger trig, boolean sync, string func returns nothing
    native DzTriggerRegisterMouseMoveEventByCode takes trigger trig, boolean sync, code funcHandle returns nothing
    native DzGetTriggerKey takes nothing returns integer
    native DzGetWheelDelta takes nothing returns integer
    native DzIsKeyDown takes integer iKey returns boolean
    native DzGetTriggerKeyPlayer takes nothing returns player
    native DzGetWindowWidth takes nothing returns integer
    native DzGetWindowHeight takes nothing returns integer
    native DzGetWindowX takes nothing returns integer
    native DzGetWindowY takes nothing returns integer
    native DzTriggerRegisterWindowResizeEvent takes trigger trig, boolean sync, string func returns nothing
    native DzTriggerRegisterWindowResizeEventByCode takes trigger trig, boolean sync, code funcHandle returns nothing
    native DzIsWindowActive takes nothing returns boolean
    //plus
    native DzDestructablePosition takes destructable d, real x, real y returns nothing
    native DzSetUnitPosition takes unit whichUnit, real x, real y returns nothing
    native DzExecuteFunc takes string funcName returns nothing
    native DzGetUnitUnderMouse takes nothing returns unit
    native DzSetUnitTexture takes unit whichUnit, string path, integer texId returns nothing
    native DzSetMemory takes integer address, real value returns nothing
    native DzSetUnitID takes unit whichUnit, integer id returns nothing
    native DzSetUnitModel takes unit whichUnit, string path returns nothing
    native DzSetWar3MapMap takes string map returns nothing
    native DzGetLocale takes nothing returns string
    native DzGetUnitNeededXP takes unit whichUnit, integer level returns integer
    //sync
    native DzTriggerRegisterSyncData takes trigger trig, string prefix, boolean server returns nothing
    native DzSyncData takes string prefix, string data returns nothing
    native DzGetTriggerSyncData takes nothing returns string
    native DzGetTriggerSyncPlayer takes nothing returns player
    //gui
    native DzFrameHideInterface takes nothing returns nothing
    native DzFrameEditBlackBorders takes real upperHeight, real bottomHeight returns nothing
    native DzFrameGetPortrait takes nothing returns integer
    native DzFrameGetMinimap takes nothing returns integer
    native DzFrameGetCommandBarButton takes integer row, integer column returns integer
    native DzFrameGetHeroBarButton takes integer buttonId returns integer
    native DzFrameGetHeroHPBar takes integer buttonId returns integer
    native DzFrameGetHeroManaBar takes integer buttonId returns integer
    native DzFrameGetItemBarButton takes integer buttonId returns integer
    native DzFrameGetMinimapButton takes integer buttonId returns integer
    native DzFrameGetUpperButtonBarButton takes integer buttonId returns integer
    native DzFrameGetTooltip takes nothing returns integer
    native DzFrameGetChatMessage takes nothing returns integer
    native DzFrameGetUnitMessage takes nothing returns integer
    native DzFrameGetTopMessage takes nothing returns integer
    native DzGetColor takes integer r, integer g, integer b, integer a returns integer
    native DzFrameSetUpdateCallback takes string func returns nothing
    native DzFrameSetUpdateCallbackByCode takes code funcHandle returns nothing
    native DzFrameShow takes integer frame, boolean enable returns nothing
    native DzCreateFrame takes string frame, integer parent, integer id returns integer
    native DzCreateSimpleFrame takes string frame, integer parent, integer id returns integer
    native DzDestroyFrame takes integer frame returns nothing
    native DzLoadToc takes string fileName returns nothing
    native DzFrameSetPoint takes integer frame, integer point, integer relativeFrame, integer relativePoint, real x, real y returns nothing
    native DzFrameSetAbsolutePoint takes integer frame, integer point, real x, real y returns nothing
    native DzFrameClearAllPoints takes integer frame returns nothing
    native DzFrameSetEnable takes integer name, boolean enable returns nothing
    native DzFrameSetScript takes integer frame, integer eventId, string func, boolean sync returns nothing
    native DzFrameSetScriptByCode takes integer frame, integer eventId, code funcHandle, boolean sync returns nothing
    native DzGetTriggerUIEventPlayer takes nothing returns player
    native DzGetTriggerUIEventFrame takes nothing returns integer
    native DzFrameFindByName takes string name, integer id returns integer
    native DzSimpleFrameFindByName takes string name, integer id returns integer
    native DzSimpleFontStringFindByName takes string name, integer id returns integer
    native DzSimpleTextureFindByName takes string name, integer id returns integer
    native DzGetGameUI takes nothing returns integer
    native DzClickFrame takes integer frame returns nothing
    native DzSetCustomFovFix takes real value returns nothing
    native DzEnableWideScreen takes boolean enable returns nothing
    native DzFrameSetText takes integer frame, string text returns nothing
    native DzFrameGetText takes integer frame returns string
    native DzFrameSetTextSizeLimit takes integer frame, integer size returns nothing
    native DzFrameGetTextSizeLimit takes integer frame returns integer
    native DzFrameSetTextColor takes integer frame, integer color returns nothing
    native DzGetMouseFocus takes nothing returns integer
    native DzFrameSetAllPoints takes integer frame, integer relativeFrame returns boolean
    native DzFrameSetFocus takes integer frame, boolean enable returns boolean
    native DzFrameSetModel takes integer frame, string modelFile, integer modelType, integer flag returns nothing
    native DzFrameGetEnable takes integer frame returns boolean
    native DzFrameSetAlpha takes integer frame, integer alpha returns nothing
    native DzFrameGetAlpha takes integer frame returns integer
    native DzFrameSetAnimate takes integer frame, integer animId, boolean autocast returns nothing
    native DzFrameSetAnimateOffset takes integer frame, real offset returns nothing
    native DzFrameSetTexture takes integer frame, string texture, integer flag returns nothing
    native DzFrameSetScale takes integer frame, real scale returns nothing
    native DzFrameSetTooltip takes integer frame, integer tooltip returns nothing
    native DzFrameCageMouse takes integer frame, boolean enable returns nothing
    native DzFrameGetValue takes integer frame returns real
    native DzFrameSetMinMaxValue takes integer frame, real minValue, real maxValue returns nothing
    native DzFrameSetStepValue takes integer frame, real step returns nothing
    native DzFrameSetValue takes integer frame, real value returns nothing
    native DzFrameSetSize takes integer frame, real w, real h returns nothing
    native DzCreateFrameByTagName takes string frameType, string name, integer parent, string template, integer id returns integer
    native DzFrameSetVertexColor takes integer frame, integer color returns nothing
    native DzOriginalUIAutoResetPoint takes boolean enable returns nothing
    native DzFrameSetPriority takes integer frame, integer priority returns nothing
    native DzFrameSetParent takes integer frame, integer parent returns nothing
    native DzFrameGetHeight takes integer frame returns real
    native DzFrameSetFont takes integer frame, string fileName, real height, integer flag returns nothing
    native DzFrameGetParent takes integer frame returns integer
    native DzFrameSetTextAlignment takes integer frame, integer align returns nothing
    native DzFrameGetName takes integer frame returns string

	native DzAPI_Map_SaveServerValue        takes player whichPlayer, string key, string value returns boolean
	native DzAPI_Map_GetServerValue         takes player whichPlayer, string key returns string
	native DzAPI_Map_Ladder_SetStat         takes player whichPlayer, string key, string value returns nothing
	native DzAPI_Map_IsRPGLadder            takes nothing returns boolean
	native DzAPI_Map_GetGameStartTime       takes nothing returns integer
	native DzAPI_Map_Stat_SetStat           takes player whichPlayer, string key, string value returns nothing
	native DzAPI_Map_GetMatchType      		takes nothing returns integer
	native DzAPI_Map_Ladder_SetPlayerStat   takes player whichPlayer, string key, string value returns nothing
	native DzAPI_Map_GetServerValueErrorCode takes player whichPlayer returns integer
	native DzAPI_Map_GetLadderLevel         takes player whichPlayer returns integer
	native DzAPI_Map_IsRedVIP               takes player whichPlayer returns boolean
	native DzAPI_Map_IsBlueVIP              takes player whichPlayer returns boolean
	native DzAPI_Map_GetLadderRank          takes player whichPlayer returns integer
	native DzAPI_Map_GetMapLevelRank        takes player whichPlayer returns integer
	native DzAPI_Map_GetGuildName           takes player whichPlayer returns string
	native DzAPI_Map_GetGuildRole           takes player whichPlayer returns integer
	native DzAPI_Map_IsRPGLobby             takes nothing returns boolean
	native DzAPI_Map_GetMapLevel            takes player whichPlayer returns integer
	native DzAPI_Map_MissionComplete        takes player whichPlayer, string key, string value returns nothing
	native DzAPI_Map_GetActivityData        takes nothing returns string
	native DzAPI_Map_GetMapConfig           takes string key returns string
	native DzAPI_Map_HasMallItem            takes player whichPlayer, string key returns boolean
	native DzAPI_Map_SavePublicArchive      takes player whichPlayer, string key, string value returns boolean
	native DzAPI_Map_GetPublicArchive       takes player whichPlayer, string key returns string
	native DzAPI_Map_UseConsumablesItem     takes player whichPlayer, string key returns nothing
	native DzAPI_Map_OrpgTrigger            takes player whichPlayer, string key returns nothing
	native DzAPI_Map_GetServerArchiveDrop   takes player whichPlayer, string key returns string
	native DzAPI_Map_GetServerArchiveEquip  takes player whichPlayer, string key returns integer
	native RequestExtraIntegerData          takes integer dataType, player whichPlayer, string param1, string param2, boolean param3, integer param4, integer param5, integer param6 returns integer
	native RequestExtraBooleanData          takes integer dataType, player whichPlayer, string param1, string param2, boolean param3, integer param4, integer param5, integer param6 returns boolean
	native RequestExtraStringData           takes integer dataType, player whichPlayer, string param1, string param2, boolean param3, integer param4, integer param5, integer param6 returns string
	native RequestExtraRealData             takes integer dataType, player whichPlayer, string param1, string param2, boolean param3, integer param4, integer param5, integer param6 returns real
	native DzAPI_Map_GetPlatformVIP         takes player whichPlayer returns integer

    native InitGameCache 					 takes string campaignFile returns gamecache
    native FlushGameCache				     takes gamecache cache returns nothing
    native SyncStoredInteger 				 takes gamecache cache, string missionKey, string key returns nothing
    native HaveStoredInteger 				 takes gamecache cache, string missionKey, string key returns boolean
    native StoreString 						 takes gamecache cache, string missionKey, string key, string value returns boolean
    native GetStoredString 					 takes gamecache cache, string missionKey, string key returns string
    native StoreInteger 					 takes gamecache cache, string missionKey, string key, integer value returns nothing
    native GetStoredInteger 				 takes gamecache cache, string missionKey, string key returns integer
    native EXNetConsume 					 takes player p,integer consume returns boolean
    native EXNetCommonConsume 				 takes player p,integer consume returns boolean
    native EXGetPlayerRealName 				 takes player p returns string
    native EXNetUseItem  					 takes player player_id,string itemid,integer amount returns boolean
    native EXNetGetTime 					 takes nothing returns string
    native EXNetStatRemoteData 				 takes integer player_id, string key ,string value returns boolean
    native EXNetSaveRemoteData 				 takes integer player_id, string Key ,string value returns boolean
    native EXNetLoadRemoteData 				 takes integer player_id, string Key returns boolean
    native EXNetIsYYHighLadder 				 takes nothing returns boolean
    native EXNetBit32And 					 takes integer v1, integer v2 returns integer
    native EXNetBit32Or 					 takes integer v1, integer v2 returns integer
    native EXNetGetYYAssistantValue 		 takes string key returns integer
    native EXGetGamePlaySecs                 takes player p returns integer
	native SyncStoredInteger				takes gamecache cache, string missionKey, string key returns nothing
	native SyncStoredReal   				takes gamecache cache, string missionKey, string key returns nothing
	native SyncStoredBoolean				takes gamecache cache, string missionKey, string key returns nothing
	native SyncStoredString 				takes gamecache cache, string missionKey, string key returns nothing

]]

mt.japi['GetTargetObject'] = japi['DzGetUnitUnderMouse']
mt.japi['GetMouseX'] = japi['DzGetMouseTerrainX']   
mt.japi['GetMouseY'] = japi['DzGetMouseTerrainY']   
mt.japi['GetMouseZ'] = japi['DzGetMouseTerrainZ']   
mt.japi['IsWindowActive'] = japi['DzIsWindowActive'] 

-- mt.japi['SetUnitModel'] = japi['DzSetUnitModel']
-- mt.japi['FrameSetPoint'] = japi['DzFrameSetPoint']
-- mt.japi['FrameClearAllPoints'] = japi['DzFrameClearAllPoints'] 
-- mt.japi['SimpleFontStringFindByName'] = japi['DzSimpleFontStringFindByName']  
-- mt.japi['LoadToc'] = japi['DzLoadToc']
-- mt.japi['FrameGetTooltip'] = japi['DzFrameGetTooltip']
-- mt.japi['GetWheelDelta'] = japi['DzGetWheelDelta']
-- mt.japi['FrameGetHeroManaBar'] = japi['DzFrameGetHeroManaBar'] 
-- mt.japi['FrameSetParent'] = japi['DzFrameSetParent'] 
-- mt.japi['GetWindowWidth'] = japi['DzGetWindowWidth'] 
-- mt.japi['FrameGetAlpha'] = japi['DzFrameGetAlpha']
-- mt.japi['CreateFrameByTagName'] = japi['DzCreateFrameByTagName']
-- mt.japi['FrameGetMinimapButton'] = japi['DzFrameGetMinimapButton']
-- mt.japi['GetMouseFocus'] = japi['DzGetMouseFocus']
-- mt.japi['FrameFindByName'] = japi['DzFrameFindByName']
-- mt.japi['DestroyFrame'] = japi['DzDestroyFrame']
-- mt.japi['FrameGetChatMessage'] = japi['DzFrameGetChatMessage'] 
-- mt.japi['FrameSetEnable'] = japi['DzFrameSetEnable'] 
-- mt.japi['GetTriggerKey'] = japi['DzGetTriggerKey']
-- mt.japi['FrameGetItemBarButton'] = japi['DzFrameGetItemBarButton']
-- mt.japi['FrameSetTexture'] = japi['DzFrameSetTexture']
-- mt.japi['FrameSetFocus'] = japi['DzFrameSetFocus']
-- mt.japi['FrameSetAllPoints'] = japi['DzFrameSetAllPoints']   
-- mt.japi['FrameEditBlackBorders'] = japi['DzFrameEditBlackBorders']
-- mt.japi['GetGameUI'] = japi['DzGetGameUI']   
-- mt.japi['FrameSetAlpha'] = japi['DzFrameSetAlpha']
-- mt.japi['FrameGetParent'] = japi['DzFrameGetParent'] 
-- mt.japi['SimpleFrameFindByName'] = japi['DzSimpleFrameFindByName']
-- mt.japi['FrameGetPortrait'] = japi['DzFrameGetPortrait']    
-- mt.japi['FrameGetHeroBarButton'] = japi['DzFrameGetHeroBarButton']
-- japi['SetMousePos'] = japi['DzSetMousePos'] 
-- japi['GetWindowHeight'] = japi['DzGetWindowHeight']
-- japi['GetWindowX'] = japi['DzGetWindowX']  
-- japi['GetWindowY'] = japi['DzGetWindowY']  
-- japi['FrameHideInterface'] = japi['DzFrameHideInterface']  
-- japi['FrameGetMinimap'] = japi['DzFrameGetMinimap']
-- japi['FrameGetCommandBarButton'] = japi['DzFrameGetCommandBarButton']    
-- japi['FrameGetHeroHPBar'] = japi['DzFrameGetHeroHPBar']   
-- japi['FrameGetUnitMessage'] = japi['DzFrameGetUnitMessage'] 
-- japi['FrameGetTopMessage'] = japi['DzFrameGetTopMessage']  
-- japi['FrameShow'] = japi['DzFrameShow']   
-- japi['CreateFrame'] = japi['DzCreateFrame'] 
-- japi['CreateSimpleFrame'] = japi['DzCreateSimpleFrame']   
-- japi['FrameSetAbsolutePoint'] = japi['DzFrameSetAbsolutePoint']
-- japi['FrameCageMouse'] = japi['DzFrameCageMouse'] 
-- japi['SimpleTextureFindByName'] = japi['DzSimpleTextureFindByName']
-- japi['FrameSetText'] = japi['DzFrameSetText']
-- japi['FrameGetText'] = japi['DzFrameGetText']
-- japi['FrameSetTextColor'] = japi['DzFrameSetTextColor']   
-- japi['FrameSetModel'] = japi['DzFrameSetModel']
-- japi['FrameSetAnimate'] = japi['DzFrameSetAnimate']
-- japi['FrameSetAnimateOffset'] = japi['DzFrameSetAnimateOffset']
-- japi['FrameSetScale'] = japi['DzFrameSetScale']
-- japi['FrameSetSize'] = japi['DzFrameSetSize']
-- japi['FrameGetHeight'] = japi['DzFrameGetHeight'] 
-- japi['FrameGetName'] = japi['DzFrameGetName']
-- japi['SetUnitTexture'] = japi['DzSetUnitTexture'] 
-- japi['SetWar3MapMap'] = japi['DzSetWar3MapMap']


local icon_file = {}
mt.japi['GetDisabledIcon'] = function(file)
    local name = (file or ""):lower():match [[[^\]+$]]
    if not name then
        return file
    end
    if not icon_file[name] then
        icon_file[name] = file
        japi.EXDclareButtonIcon(file) 
    elseif icon_file[name] ~= file then
        log.error('同一个暗图标对应了多个明图标', file, icon_file[name])
    end
    return [[ReplaceableTextures\CommandButtonsDisabled\DIS]] .. name
end


local pass_file = {}
mt.japi['GetPassiveIcon'] = function(file)
    local name = (file or ""):lower():match [[[^\]+$]]
    if not name then
        return file
    end
    local pass = [[ReplaceableTextures\PassiveButtons\pas]] .. name .. ".blp"
    if not pass_file[name] then 
        pass_file[name] = pass
        japi.EXBlendButtonIcon("core\\passiveblender.blp", file, pass)
    end 
    return pass
	-- local file_name = file:match [[[^\]+$]]
	-- if not file_name then
	-- 	return ''
	-- end
	-- if not pass_file[file_name] then
	-- 	pass_file[file_name] = file
	-- 	japi.EXDclareButtonIcon(file)
	-- elseif pass_file[file_name] ~= file then
	-- 	log.info('[Warning]同一个暗图标对应了多个明图标', file, pass_file[file_name])
	-- end
	-- return [[ReplaceableTextures\CommandButtonsDisabled\DIS]] .. file_name
end

--- 遍历控件子级UI
mt.japi['FrameGetChildlist'] = function (handle)
    local list = {}
    if handle ~= 0 then 
        local child = japi.FrameGetChilds(handle, 0)
        while child ~= 0 do 
            list[#list + 1] = child 
            child = japi.FrameGetChilds(handle, child)
        end 
    end
    return list 
end

---加载FDF文件
---@params str fdf模板
local local_id  = jass.GetPlayerId(jass.GetLocalPlayer()) + 1
local toc_name  = ("ui\\template%02d.toc"):format(local_id)
local loaded    = ("ui\\template%02d.fdf"):format(local_id)
local loading   = ("ui\\custom%02d.fdf"):format(local_id)

storm.save(toc_name,'\r\n')
storm.save(loaded,'\r\n')
storm.save(loading,'\r\n')
function load_fdf(fdf)
    local loadedfdf = storm.load(loaded) or ""
    storm.save(loaded, loadedfdf .. '\n'.. fdf)
    storm.save(loading,'\n' .. fdf)
    storm.save(toc_name, loading .. '\r\n')
    japi.LoadToc(toc_name)
end

-- Blp剪切 - 扇形
local blp_sector = { }
function blpcut_sector( path, x, y, r, angle, section)
    x = math.modf(x)
    y = math.modf(y)
    r = math.modf(r)
    angle = math.modf(angle or 0)
    section = math.modf(section or 360)
    local key = string.format("%i_%i_%i_%i_%i", x, y, r, angle, section)
    local newpath = path:gsub( [[.[^.]+$]] , function( name )
        return key .. name
    end )
    if not blp_sector[newpath] then
        blp_sector[newpath] = true
        japi.EXBlpSector(path, newpath, x, y, r, angle, section)
    end
    return newpath
end

-- Blp剪切 - 矩形
local blp_rect = { }
function blpcut_rect(path, left, top, right, bottom)
    left = math.modf(left)
    top = math.modf(top)
    right = math.modf(right)
    bottom = math.modf(bottom)
    local key = string.format("%i_%i_%i_%i", left, top, right, bottom)
    local newpath  = path:gsub( [[.[^.]+$]] , function( name )
        return key .. name
    end )
    if not blp_rect[newpath] then
        blp_rect[newpath] = true
        japi.EXBlpRect(path, newpath, left, top, right, bottom)
    end
    return newpath
end

local symbols        = require 'symbol.unit'
function get_unit_slk(handle, name, default)
    local result 
    local unitid
    if type(handle)=='string' then
        unitid = symbols[handle]
    else
        unitid = jass.GetUnitTypeId(handle)
    end
    if unitid and unitid ~= 0 then 
        local object = slk.unit[unitid]
        if object then 
            result = object[name]
        end
    end 
    if result == nil then
        return default
    end
    if type(default) == 'number' then
        return tonumber(result) or default
    end
    return result
end 
-- 定义PRD函数
function PRD(chance, counter, maxCounter)
    local threshold = chance / (counter + 1)
    return counter >= maxCounter or math.random() <= threshold
end 

-- 设置单位类型name (必须要在使用这个单位类型之前设置)
mt.japi['SetUnitBaseName'] = function (unitcode, name)
    if japi.EXSetUnitArrayString and unitcode and name then 
        japi.EXSetUnitArrayString( unitcode, 10, 0, name)
        japi.EXSetUnitInteger(unitcode, 10, 1)
    end
end

-- 设置单位类型称谓 (必须要在使用这个单位类型之前设置)
mt.japi['SetUnitBaseProperName'] = function (unitcode, name)
    if japi.EXSetUnitArrayString and unitcode and name and jass.IsHeroUnitId(unitcode) then 
        japi.EXSetUnitArrayString( unitcode, 61, 0, name)
        japi.EXSetUnitInteger(unitcode, 61, 1)
    end
end

-- 设置单位类型art (必须要在使用这个单位类型之前设置)
mt.japi['SetUnitBaseArt'] = function (unitcode, art)
    if japi.EXSetUnitArrayString and unitcode and art then 
        japi.EXSetUnitArrayString( unitcode, 146, 0, art)
        japi.EXSetUnitInteger(unitcode, 146, 1)
    end
end

-- 设置单位类型tip (必须要在使用这个单位类型之前设置)
mt.japi['SetUnitBaseTip'] = function (unitcode, tip)
    if japi.EXSetUnitArrayString and unitcode and tip then 
        japi.EXSetUnitArrayString( unitcode, 151, 0, tip)
        japi.EXSetUnitInteger(unitcode, 151, 1)
    end
end

-- 设置单位类型ubertip (必须要在使用这个单位类型之前设置)
mt.japi['SetUnitBaseUbertip'] = function (unitcode, ubertip)
    if japi.EXSetUnitArrayString and unitcode and ubertip then 
        japi.EXSetUnitArrayString( unitcode, 154, 0, ubertip)
        japi.EXSetUnitInteger(unitcode, 154, 1)
    end
end

mt.japi['IsWindowActive'] = function()
    return true 
end 

mt.japi['DzIsWindowActive'] = function()
    return true 
end 

